<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use app\service\roominfoService;
use think\Db;
class Staff extends Common{
	//员工列表
	public function staffList(){
		$com = $this->pidTokenId();
		$whe['community_id'] = array('in',$com['community_id']);
		$communitylist=(new roominfoService())->communityList($whe);;
		$this->assign('communitylist',$communitylist);
		$stafflist=Db::table("v2_staff")->where($whe)->order("id desc")->select();
		
		$this->assign("list",$stafflist);
		return $this->fetch("v2_staff/staff_list");
	}

	//员工添加
	public function staffAdd(){
		if(request()->isPost()){
			//获取表单数据
            $data = request()->post();
           	$community=Db::name('community')->where('community_id',$data["community_id"])->find();
           	//echo "<pre>";
           //var_dump($data);die;
           	$data["community_name"]=$community["community_name"];
           	$data["entry_time"]=strtotime($data["entry_time"]);
           	//$data["work_on_time"]=strtotime($data["work_on_time"])-strtotime(date('Y-m-d'));
           	//$data["work_off_time"]=strtotime($data["work_off_time"])-strtotime(date('Y-m-d'));
           	$data["create_time"]=time();
           	$res=Db::table("v2_staff")->insert($data);
           	if($res){
                $this->success('新增员工成功',url('staffAdd'));
            }else{
                $this->error('新增员工成功',url('staffAdd'));
            }
		}else{
			//查询小区信息
	        $com = $this->pidTokenId();
	        $where['community_id'] = array('in',$com['community_id']);
	        $communitylist = (new roominfoService())->communityList($where);
	        $this->assign('communitylist',$communitylist);
			return $this->fetch("v2_staff/staff_add");
		}
	}

	//考勤记录
	public function attendanceList(){
		$com = $this->pidTokenId();
		$whe['community_id'] = array('in',$com['community_id']);
		$communitylist=(new roominfoService())->communityList($whe);;
		$this->assign('communitylist',$communitylist);
		$attendancelist=Db::table("v2_attendance")->where($whe)->order("id desc")->select();
		
		$this->assign("list",$attendancelist);
		return $this->fetch("v2_staff/attendance_list");
	}

	public function attendanceAdd(){
		if(request()->isPost()){
			//获取表单数据
            $data = request()->post();
            //var_dump($data);die;
           	$staffinfo=Db::name('v2_staff')->where('id',$data["staff_id"])->find();

           	$data["staff_name"]=$staffinfo["staff_name"];
           	$data["community_id"]=$staffinfo["community_id"];
           	$data["community_name"]=$staffinfo["community_name"];
           	$data["days"]=strtotime($data["days"]);
           	
           	$data["create_time"]=time();
           	$res=Db::table("v2_attendance")->insert($data);
           	if($res){
                $this->success('新增考勤成功',url('staffAdd'));
            }else{
                $this->error('新增考勤失败',url('staffAdd'));
            }
		}else{
			//查询小区信息
	        $com = $this->pidTokenId();
	        $where['community_id'] = array('in',$com['community_id']);
	        $communitylist = (new roominfoService())->communityList($where);
	        $this->assign('communitylist',$communitylist);
	        $stafflist=Db::table("v2_staff")->where($where)->order("id desc")->select();
			$this->assign("stafflist",$stafflist);
			return $this->fetch("v2_staff/attendance_add");
		}
	}
}